from gamestate import * 
    


class Statistics(object):
    max_radiation = -1
    max_radiation_temperature = -1
    max_radiation_tuple = []
    
    min_radiation = 999999999
    min_radation_temp = 999999999
    min_radiation_tuple = []
    
    max_temperature = -1
    max_temperature_radiation = -1
    max_temperature_tuple = []
    
    min_temperature= 999999999
    min_temperature_radiation = 999999999
    min_temperature_tuple = [] 
    
    temperature_interval_top = 720
    temperature_intverval_bottom = 671
    temperature_interval_size = 0
    temperature_interval_classes = [0,0,0,0,0,0,0,0,0,0]
    
    radiation_interval_top = 8865
    radiation_intverval_bottom = 2408
    radiation_interval_size = 0
    radiation_interval_classes = [0,0,0,0,0,0,0,0,0,0]

def printStatistics():    
    print "max_radiation: " + str(Statistics.max_radiation)
    print "max_radiation_temperature: " + str(Statistics.max_radiation_temperature)
    print "max_radiation_tuple" + str(Statistics.max_radiation_tuple)
    
    print "min_radiation: " + str(Statistics.min_radiation)
    print "min_radation_temp: " + str (Statistics.min_radation_temp)
    print "min_radiation_tuple" + str(Statistics.min_radiation_tuple)
    
    print "radation_distribution" + str(Statistics.radiation_interval_classes)
        
    print "max_temperature: " + str(Statistics.max_temperature)
    print "max_temperature_radiation: " + str(Statistics.max_temperature_radiation)
    print "max_temperature_tuple" + str(Statistics.max_temperature_tuple)
    
    print "min_temperature: " + str(Statistics.min_temperature)
    print "min_temperature_radiation: " + str(Statistics.min_temperature_radiation)
    print "min_temperature_tuple" + str(Statistics.min_temperature_tuple) 
    
    print "temperature_distribution" + str(Statistics.temperature_interval_classes)

def performCalcForOneTuple(tuple):
    GlobalStates.AddCounterMeasureAction(gamestate.actions[tuple[0]-1])
    gamestate.tick()
    GlobalStates.AddCounterMeasureAction(gamestate.actions[tuple[1]-1])
    gamestate.tick()
    GlobalStates.AddCounterMeasureAction(gamestate.actions[tuple[2]-1])
    gamestate.tick()
    GlobalStates.AddCounterMeasureAction(gamestate.actions[tuple[3]-1])
    gamestate.tick()
    GlobalStates.AddCounterMeasureAction(gamestate.actions[tuple[4]-1])
    gamestate.tick()
    GlobalStates.AddCounterMeasureAction(gamestate.actions[tuple[5]-1])
    gamestate.tick()
    GlobalStates.AddCounterMeasureAction(gamestate.actions[tuple[6]-1])
    gamestate.tick()
    GlobalStates.AddCounterMeasureAction(gamestate.actions[tuple[7]-1])
    gamestate.tick()
    GlobalStates.AddCounterMeasureAction(gamestate.actions[tuple[8]-1])
    gamestate.tick() 
        
    if GlobalStates.Radiation_Level > Statistics.max_radiation:
        Statistics.max_radiation = GlobalStates.Radiation_Level
        Statistics.max_radiation_temperature = GlobalStates.Core_Temperature
        Statistics.max_radiation_tuple = tuple
        
    if GlobalStates.Radiation_Level < Statistics.min_radiation:
        Statistics.min_radiation = GlobalStates.Radiation_Level
        Statistics.min_radiation_temperature = GlobalStates.Core_Temperature
        Statistics.min_radiation_tuple = tuple
        
    if GlobalStates.Core_Temperature > Statistics.max_temperature:
        Statistics.max_temperature = GlobalStates.Core_Temperature
        Statistics.max_temperature_radiation = GlobalStates.Radiation_Level
        Statistics.max_temperatur_tuple = tuple
        
    if GlobalStates.Core_Temperature < Statistics.min_temperature:
        Statistics.min_temperature = GlobalStates.Core_Temperature
        Statistics.min_temperature_radiation = GlobalStates.Radiation_Level
        Statistics.min_temperature_tuple = tuple
        
    temperature_index = 0
    temperature_within_bounds = True;
    temperature_low_bounds = Statistics.temperature_intverval_bottom
    temperature_top_bounds = Statistics.temperature_intverval_bottom + Statistics.temperature_interval_size
    
    while temperature_within_bounds == True:
        if temperature_index >= 10:
            temperature_within_bounds = False
        elif GlobalStates.Core_Temperature >= temperature_low_bounds and GlobalStates.Core_Temperature < temperature_top_bounds:
            Statistics.temperature_interval_classes[temperature_index] += 1
            temperature_within_bounds = False

        temperature_low_bounds += Statistics.temperature_interval_size
        temperature_top_bounds += Statistics.temperature_interval_size
        temperature_index += 1
    
    radiation_index = 0
    radiation_within_bounds = True;
    radiation_low_bounds = Statistics.radiation_intverval_bottom
    radiation_top_bounds = Statistics.radiation_intverval_bottom + Statistics.radiation_interval_size
    
    while radiation_within_bounds == True:
        if radiation_index >= 10:
            radiation_within_bounds = False 
        elif GlobalStates.Radiation_Level >= radiation_low_bounds and GlobalStates.Radiation_Level < radiation_top_bounds:
            Statistics.radiation_interval_classes[radiation_index] += 1
            radiation_within_bounds = False

    radiation_low_bounds += Statistics.radiation_interval_size
    radiation_top_bounds += Statistics.radiation_interval_size
    radiation_index += 1

    GlobalStates.ResetGlobalStates()
                                                                                    
if __name__ == "__main__":
    gamestate = GameState()  
    DebugLogger.DebugMode = False  
                                
    Statistics.temperature_interval_size= ( Statistics.temperature_interval_top - Statistics.temperature_intverval_bottom ) / 10
    Statistics.radiation_interval_size = ( Statistics.radiation_interval_top - Statistics.radiation_intverval_bottom ) / 10
        
    counter = 0;
    pool = tuple(range(1,10))
    n = len(pool)
    r = n    
    indices = range(n)
    cycles = range(n, n-r, -1)
    myTuple = tuple(pool[i] for i in indices[:r])
    performCalcForOneTuple(myTuple)
    printStatistics()    
    stop = False
    while n and stop == False:
        for i in reversed(range(r)):
            cycles[i] -= 1
            if cycles[i] == 0:
                indices[i:] = indices[i+1:] + indices[i:i+1]
                cycles[i] = n - i
            else:
                j = cycles[i]
                indices[i], indices[-j] = indices[-j], indices[i]
                myTuple = tuple(pool[i] for i in indices[:r]) 
                if counter % 1000 == 0:
                    print myTuple
                counter += 1
                performCalcForOneTuple(myTuple)                       
                break
        else:
            stop = True
            
    print "finished calculating"
    printStatistics()

